package com.xyongfeng.foodManagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyongfeng.foodManagement.common.BaseResponse;
import com.xyongfeng.foodManagement.common.ErrorCode;
import com.xyongfeng.foodManagement.common.ResultUtils;
import com.xyongfeng.foodManagement.domain.AdminInfo;
import com.xyongfeng.foodManagement.domain.FoodInfo;
import com.xyongfeng.foodManagement.domain.group.AddGroup;
import com.xyongfeng.foodManagement.domain.group.EditGroup;
import com.xyongfeng.foodManagement.domain.group.LoginGourp;
import com.xyongfeng.foodManagement.domain.group.NormalGroup;
import com.xyongfeng.foodManagement.domain.request.IdReqeust;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoSimpleVO;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoVO;
import com.xyongfeng.foodManagement.exception.BusinessException;
import com.xyongfeng.foodManagement.exception.ThrowUtils;
import com.xyongfeng.foodManagement.service.AdminInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 控制管理员的接口
 *
 * @author xyongfeng
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminInfoService adminInfoService;

    @PostMapping("/login")
    public BaseResponse<AdminInfoVO> adminLogin(@RequestBody @Validated({LoginGourp.class, NormalGroup.class}) AdminInfo adminInfo,
                                                HttpServletRequest request) {
        AdminInfoVO adminInfoVO = adminInfoService.login(adminInfo.getAdminUsername(), adminInfo.getAdminPassword(), request);
        ThrowUtils.throwIf(adminInfoVO == null, new BusinessException(ErrorCode.OPERATION_ERROR, "账号或密码错误"));

        return ResultUtils.success(adminInfoVO);
    }

    @GetMapping("/logout")
    public BaseResponse<AdminInfoVO> adminLogout(HttpServletRequest request) {

        AdminInfoVO logout = adminInfoService.logout(request);
        ThrowUtils.throwIf(logout == null, new BusinessException(ErrorCode.OPERATION_ERROR, "退出失败"));

        return ResultUtils.success(logout);
    }

    @GetMapping
    public BaseResponse<List<AdminInfoVO>> selectAll(HttpServletRequest request) {
        AdminInfoVO loginAdmin = adminInfoService.getLoginAdmin(request);
        ThrowUtils.throwIf(loginAdmin == null, new BusinessException(ErrorCode.NOT_LOGIN_ERROR));

        return ResultUtils.success(adminInfoService.selectAll());
    }

    @GetMapping("/simple")
    public BaseResponse<List<AdminInfoSimpleVO>> selectSimpleAll() {

        return ResultUtils.success(adminInfoService.selectSimpleAll());
    }

    @RequiresPermissions("admin")
    @PostMapping
    public BaseResponse<AdminInfoVO> insert(@RequestBody @Validated({AddGroup.class, NormalGroup.class}) AdminInfo adminInfo) {

        // 不能添加重复账号的
        ThrowUtils.throwIf(adminInfoService
                        .getOne(new QueryWrapper<AdminInfo>().eq("admin_username", adminInfo.getAdminUsername())) != null,
                new BusinessException(ErrorCode.PARAMS_ERROR.getCode(), "账号已存在"));


        adminInfoService.save(adminInfo);

        return ResultUtils.success(adminInfo.convertToVO());
    }

    @RequiresPermissions("admin")
    @DeleteMapping("/{id}")
    public BaseResponse<AdminInfoVO> deleteById(@PathVariable Integer id) {

        AdminInfo adminInfoById = adminInfoService.getById(id);
        ThrowUtils.throwIf(adminInfoById.getAdminId().equals(id), ErrorCode.PARAMS_ERROR, "不能删除自己");
        boolean b = adminInfoService.removeById(id);
        if (!b) return ResultUtils.error(ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(adminInfoById.convertToVO());
    }

    @RequiresPermissions("admin")
    @PutMapping
    public BaseResponse<AdminInfoVO> updateById(@RequestBody @Validated({EditGroup.class, NormalGroup.class}) AdminInfo adminInfo) {

        boolean adminInfoById = adminInfoService.updateById(adminInfo);
        if (!adminInfoById) return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(), "修改失败");
        adminInfo = adminInfoService.getById(adminInfo.getAdminId());

        return ResultUtils.success(adminInfo.convertToVO());
    }


}
